package edu.unindra.elearning.form.addin;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;

import usu.widget.ButtonGlass;
import usu.widget.glass.TextBoxGlass;
import edu.unindra.elearning.model.DefaultMateri;
import edu.unindra.elearning.model.Materi;
import edu.unindra.elearning.util.PanelBackGround;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.io.File;
import java.awt.Font;

public class ELearningFormMainAdminMateriEdit extends PanelBackGround {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel6;

	private ButtonGlass btnSubmit;
	private ButtonGlass btnCancel;
	private ButtonGlass btnBrowser;

	private TextBoxGlass txtKelas;
	private TextBoxGlass txtPathSoal;
	private TextBoxGlass txtStatus;
	private TextBoxGlass txtMateri;

	private File file;
	private JFileChooser choser;

	private String invalidMessage = "";

	private Materi materi;

	public ELearningFormMainAdminMateriEdit() {

		choser = new JFileChooser();

		initComponents();
	}

	public String getInvalidMessage() {
		return invalidMessage;
	}

	public ButtonGlass getBtnSubmit() {
		return btnSubmit;
	}

	public ButtonGlass getBtnCancel() {
		return btnCancel;
	}

	private void initComponents() {
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel4 = new JLabel();
		jLabel6 = new JLabel();

		txtMateri = new TextBoxGlass();
		txtPathSoal = new TextBoxGlass();
		txtKelas = new TextBoxGlass();
		txtStatus = new TextBoxGlass();

		btnBrowser = new ButtonGlass();
		btnBrowser.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnBrowser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttonBrowserActionPerformed(e);
			}
		});

		btnSubmit = new ButtonGlass();
		btnSubmit.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnCancel = new ButtonGlass();
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 11));

		setBorder(javax.swing.BorderFactory.createLineBorder(
				new java.awt.Color(0, 0, 0), 3));
		setMinimumSize(new java.awt.Dimension(485, 339));
		setPreferredSize(new Dimension(485, 239));

		jLabel1.setDisplayedMnemonic('I');
		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel1.setForeground(new java.awt.Color(255, 255, 255));
		jLabel1.setText("Path Soal");

		jLabel2.setDisplayedMnemonic('P');
		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel2.setForeground(new java.awt.Color(255, 255, 255));
		jLabel2.setText("Kelas");

		jLabel3.setDisplayedMnemonic('N');
		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel3.setForeground(new java.awt.Color(255, 255, 255));
		jLabel3.setText("Tipe Materi");

		jLabel4.setDisplayedMnemonic('K');
		jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel4.setForeground(new java.awt.Color(255, 255, 255));
		jLabel4.setText("Status");

		btnBrowser.setMnemonic('B');
		btnBrowser.setText("Browser");
		btnBrowser.setRoundRect(true);

		btnSubmit.setMnemonic('T');
		btnSubmit.setText("Ubah");
		btnSubmit.setRoundRect(true);

		btnCancel.setMnemonic('C');
		btnCancel.setText("Batal");
		btnCancel.setRoundRect(true);

		txtMateri.setForeground(new java.awt.Color(255, 255, 255));
		txtMateri.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtMateri.setFocusAccelerator('M');
		txtMateri.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtPathSoal.setForeground(new java.awt.Color(255, 255, 255));
		txtPathSoal.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtPathSoal.setFocusAccelerator('I');
		txtPathSoal.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtKelas.setForeground(new java.awt.Color(255, 255, 255));
		txtKelas.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtKelas.setFocusAccelerator('N');
		txtKelas.setFont(new java.awt.Font("Tahoma", 1, 11));
		txtKelas.setEnabled(false);

		txtStatus.setForeground(new java.awt.Color(255, 255, 255));
		txtStatus.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtStatus.setFocusAccelerator('K');
		txtStatus.setFont(new java.awt.Font("Tahoma", 1, 11));

		jLabel6.setFont(new java.awt.Font("Tahoma", 1, 24));
		jLabel6.setForeground(new java.awt.Color(255, 255, 255));
		jLabel6.setText("Ubah Materi Baru");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING,
																				false)
																				.addComponent(
																						jLabel4,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel3,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel2,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.addComponent(
																						jLabel1))
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						txtKelas,
																						GroupLayout.DEFAULT_SIZE,
																						391,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtMateri,
																						0,
																						391,
																						Short.MAX_VALUE)
																				.addComponent(
																						txtStatus,
																						GroupLayout.DEFAULT_SIZE,
																						391,
																						Short.MAX_VALUE)
																				.addGroup(
																						Alignment.TRAILING,
																						layout.createSequentialGroup()
																								.addComponent(
																										txtPathSoal,
																										GroupLayout.DEFAULT_SIZE,
																										271,
																										Short.MAX_VALUE)
																								.addPreferredGap(
																										ComponentPlacement.UNRELATED)
																								.addComponent(
																										btnBrowser,
																										GroupLayout.PREFERRED_SIZE,
																										110,
																										GroupLayout.PREFERRED_SIZE))))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		btnCancel,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addComponent(
																		btnSubmit,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE))
												.addComponent(
														jLabel6,
														GroupLayout.DEFAULT_SIZE,
														459, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(11)
								.addComponent(jLabel6)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														txtMateri,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(jLabel2)
												.addComponent(
														txtKelas,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														txtPathSoal,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnBrowser,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(jLabel4)
												.addComponent(
														txtStatus,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														btnSubmit,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnCancel,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));
		this.setLayout(layout);
	}

	protected void buttonBrowserActionPerformed(ActionEvent e) {
		int ren = choser.showOpenDialog(this);
		if (ren == JFileChooser.APPROVE_OPTION) {
			file = choser.getSelectedFile();
			txtPathSoal.setText(file.getAbsolutePath());
		}
	}

	/**
	 * reset all text component
	 */
	public void reset() {
		txtMateri.setText("");
		txtStatus.setText("");
		txtPathSoal.setText("");
		txtKelas.setText("");
	}

	public boolean isMateriValid() {
		if (txtPathSoal.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "Path Soal masih kosong";
			return false;
		}

		if (txtKelas.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "Kelas masih kosong";
			return false;
		}

		if (txtStatus.getText().trim().equalsIgnoreCase("")) {
			invalidMessage = "Status Soal masih kosong";
			return false;
		}
		return true;
	}

	public Materi getMateri() {
		DefaultMateri m = new DefaultMateri();
		m.setMateri(txtMateri.getText());
		m.setKelas(txtKelas.getText());
		m.setPath(txtPathSoal.getText());
		m.setStatus(txtStatus.getText());

		return m;

	}

	public void setMateri(Materi m) {
		materi = m;
		load();
	}

	private void load() {
		if (materi == null) {
			throw new IllegalArgumentException();
		}

		txtMateri.setText(materi.getMateri());
		txtKelas.setText(materi.getKelas());
		txtPathSoal.setText(materi.getPath());
		txtStatus.setText(materi.getStatus());
	}
}
